/*     */ package net.gxf.java2dgame.tetris;
/*     */ 
/*     */ import java.awt.Color;
/*     */ import java.awt.Dimension;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.event.ActionEvent;
/*     */ import java.awt.event.ActionListener;
/*     */ import java.awt.event.KeyAdapter;
/*     */ import java.awt.event.KeyEvent;
/*     */ import javax.swing.JLabel;
/*     */ import javax.swing.JPanel;
/*     */ import javax.swing.Timer;
/*     */ 
/*     */ public class Board extends JPanel
/*     */   implements ActionListener
/*     */ {
/*  18 */   private final int BoardWidth = 10;
/*  19 */   private final int BoardHeight = 22;
/*     */   private Timer timer;
/*  22 */   private boolean isFallingFinished = false;
/*  23 */   private boolean isStarted = false;
/*  24 */   private boolean isPaused = false;
/*  25 */   private int numLinesRemoved = 0;
/*  26 */   private int curX = 0;
/*  27 */   private int curY = 0;
/*     */   private JLabel statusbar;
/*     */   private Shape curPiece;
/*     */   private Utils.ShapeType[] board;
/*     */ 
/*     */   public Board(Tetris parent)
/*     */   {
/*  37 */     setFocusable(true);
/*  38 */     this.curPiece = new Shape();
/*  39 */     this.timer = new Timer(400, this);
/*  40 */     this.timer.start();
/*     */ 
/*  42 */     this.statusbar = parent.getStatusBar();
/*  43 */     this.board = new Utils.ShapeType['Ü'];
/*  44 */     addKeyListener(new TAdapter());
/*  45 */     clearBoard();
/*     */   }
/*     */ 
/*     */   public void actionPerformed(ActionEvent e) {
/*  49 */     if (this.isFallingFinished) {
/*  50 */       this.isFallingFinished = false;
/*  51 */       newPiece();
/*     */     } else {
/*  53 */       oneLineDown();
/*     */     }
/*     */   }
/*     */ 
/*     */   int squareWidth()
/*     */   {
/*  59 */     return (int)getSize().getWidth() / 10;
/*     */   }
/*     */ 
/*     */   int squareHeight() {
/*  63 */     return (int)getSize().getHeight() / 22;
/*     */   }
/*     */ 
/*     */   Utils.ShapeType shapeAt(int x, int y)
/*     */   {
/*  73 */     return this.board[(y * 10 + x)];
/*     */   }
/*     */ 
/*     */   public void start()
/*     */   {
/*  78 */     if (this.isPaused) {
/*  79 */       return;
/*     */     }
/*  81 */     this.isStarted = true;
/*  82 */     this.isFallingFinished = false;
/*  83 */     this.numLinesRemoved = 0;
/*  84 */     clearBoard();
/*     */ 
/*  86 */     newPiece();
/*  87 */     this.timer.start();
/*     */   }
/*     */ 
/*     */   private void pause() {
/*  91 */     if (!this.isStarted) {
/*  92 */       return;
/*     */     }
/*  94 */     this.isPaused = (!this.isPaused);
/*  95 */     if (this.isPaused) {
/*  96 */       this.timer.stop();
/*  97 */       this.statusbar.setText("paused");
/*     */     } else {
/*  99 */       this.timer.start();
/* 100 */       this.statusbar.setText(String.valueOf(this.numLinesRemoved));
/*     */     }
/* 102 */     repaint();
/*     */   }
/*     */ 
/*     */   public void paint(Graphics g)
/*     */   {
/* 107 */     super.paint(g);
/*     */ 
/* 109 */     Dimension size = getSize();
/* 110 */     int boardTop = (int)size.getHeight() - 22 * squareHeight();
/*     */ 
/* 114 */     for (int i = 0; i < 22; i++) {
/* 115 */       for (int j = 0; j < 10; j++) {
/* 116 */         Utils.ShapeType shape = shapeAt(j, 22 - i - 1);
/* 117 */         if (shape != Utils.ShapeType.NoShape) {
/* 118 */           drawSquare(g, 0 + j * squareWidth(), boardTop + i * squareHeight(), shape);
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 123 */     if (this.curPiece.getShape() != Utils.ShapeType.NoShape)
/* 124 */       for (int i = 0; i < 4; i++) {
/* 125 */         int x = this.curX + this.curPiece.x(i);
/* 126 */         int y = this.curY - this.curPiece.y(i);
/* 127 */         drawSquare(g, 0 + x * squareWidth(), boardTop + (22 - y - 1) * squareHeight(), this.curPiece.getShape());
/*     */       }
/*     */   }
/*     */ 
/*     */   private void dropDown()
/*     */   {
/* 133 */     int newY = this.curY;
/* 134 */     while ((newY > 0) && 
/* 135 */       (tryMove(this.curPiece, this.curX, newY - 1)))
/*     */     {
/* 137 */       newY--;
/*     */     }
/* 139 */     pieceDropped();
/*     */   }
/*     */ 
/*     */   private void oneLineDown() {
/* 143 */     if (!tryMove(this.curPiece, this.curX, this.curY - 1))
/* 144 */       pieceDropped();
/*     */   }
/*     */ 
/*     */   private void clearBoard()
/*     */   {
/* 149 */     for (int i = 0; i < 220; i++)
/* 150 */       this.board[i] = Utils.ShapeType.NoShape;
/*     */   }
/*     */ 
/*     */   private void pieceDropped()
/*     */   {
/* 157 */     for (int i = 0; i < 4; i++) {
/* 158 */       int x = this.curX + this.curPiece.x(i);
/* 159 */       int y = this.curY - this.curPiece.y(i);
/* 160 */       this.board[(y * 10 + x)] = this.curPiece.getShape();
/*     */     }
/*     */ 
/* 163 */     removeFullLines();
/*     */ 
/* 165 */     if (!this.isFallingFinished)
/* 166 */       newPiece();
/*     */   }
/*     */ 
/*     */   private void newPiece()
/*     */   {
/* 173 */     this.curPiece.setRandomShape();
/* 174 */     this.curX = 6;
/* 175 */     this.curY = (21 + this.curPiece.minY());
/*     */ 
/* 177 */     if (!tryMove(this.curPiece, this.curX, this.curY)) {
/* 178 */       this.curPiece.setShape(Utils.ShapeType.NoShape);
/* 179 */       this.timer.stop();
/* 180 */       this.isStarted = false;
/* 181 */       this.statusbar.setText("game over");
/*     */     }
/*     */   }
/*     */ 
/*     */   private boolean tryMove(Shape newPiece, int newX, int newY)
/*     */   {
/* 192 */     for (int i = 0; i < 4; i++) {
/* 193 */       int x = newX + newPiece.x(i);
/* 194 */       int y = newY - newPiece.y(i);
/* 195 */       if ((x < 0) || (x >= 10) || (y < 0) || (y >= 22))
/* 196 */         return false;
/* 197 */       if (shapeAt(x, y) != Utils.ShapeType.NoShape) {
/* 198 */         return false;
/*     */       }
/*     */     }
/* 201 */     this.curPiece = newPiece;
/* 202 */     this.curX = newX;
/* 203 */     this.curY = newY;
/* 204 */     repaint();
/* 205 */     return true;
/*     */   }
/*     */ 
/*     */   private void removeFullLines() {
/* 209 */     int numFullLines = 0;
/*     */ 
/* 211 */     for (int i = 21; i >= 0; i--) {
/* 212 */       boolean lineIsFull = true;
/*     */ 
/* 214 */       for (int j = 0; j < 10; j++) {
/* 215 */         if (shapeAt(j, i) == Utils.ShapeType.NoShape) {
/* 216 */           lineIsFull = false;
/* 217 */           break;
/*     */         }
/*     */       }
/*     */ 
/* 221 */       if (lineIsFull) {
/* 222 */         numFullLines++;
/* 223 */         for (int k = i; k < 21; k++) {
/* 224 */           for (int j = 0; j < 10; j++) {
/* 225 */             this.board[(k * 10 + j)] = shapeAt(j, k + 1);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 230 */     if (numFullLines > 0) {
/* 231 */       this.numLinesRemoved += numFullLines;
/* 232 */       this.statusbar.setText(String.valueOf(this.numLinesRemoved));
/* 233 */       this.isFallingFinished = true;
/* 234 */       this.curPiece.setShape(Utils.ShapeType.NoShape);
/* 235 */       repaint();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void drawSquare(Graphics g, int x, int y, Utils.ShapeType shape) {
/* 240 */     Color color = Utils.colors[shape.ordinal()];
/*     */ 
/* 242 */     g.setColor(color);
/* 243 */     g.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2);
/*     */ 
/* 245 */     g.setColor(color.brighter());
/* 246 */     g.drawLine(x, y + squareHeight() - 1, x, y);
/* 247 */     g.drawLine(x, y, x + squareWidth() - 1, y);
/*     */ 
/* 249 */     g.setColor(color.darker());
/* 250 */     g.drawLine(x + 1, y + squareHeight() - 1, x + squareWidth() - 1, y + squareHeight() - 1);
/* 251 */     g.drawLine(x + squareWidth() - 1, y + squareHeight() - 1, x + squareWidth() - 1, y + 1);
/*     */   }
/*     */ 
/*     */   class TAdapter extends KeyAdapter {
/*     */     TAdapter() {
/*     */     }
/*     */ 
/*     */     public void keyPressed(KeyEvent e) {
/* 259 */       if ((!Board.this.isStarted) || (Board.this.curPiece.getShape() == Utils.ShapeType.NoShape)) {
/* 260 */         return;
/*     */       }
/*     */ 
/* 263 */       int keycode = e.getKeyCode();
/*     */ 
/* 265 */       if ((keycode == 112) || (keycode == 80)) {
/* 266 */         Board.this.pause();
/* 267 */         return;
/*     */       }
/*     */ 
/* 270 */       if (Board.this.isPaused) {
/* 271 */         return;
/*     */       }
/* 273 */       switch (keycode) {
/*     */       case 37:
/* 275 */         Board.this.tryMove(Board.this.curPiece, Board.this.curX - 1, Board.this.curY);
/* 276 */         break;
/*     */       case 39:
/* 278 */         Board.this.tryMove(Board.this.curPiece, Board.this.curX + 1, Board.this.curY);
/* 279 */         break;
/*     */       case 40:
/* 281 */         Board.this.tryMove(Board.this.curPiece.rotateRight(), Board.this.curX, Board.this.curY);
/* 282 */         break;
/*     */       case 38:
/* 284 */         Board.this.tryMove(Board.this.curPiece.rotateLeft(), Board.this.curX, Board.this.curY);
/* 285 */         break;
/*     */       case 32:
/* 287 */         Board.this.dropDown();
/* 288 */         break;
/*     */       case 100:
/* 290 */         Board.this.oneLineDown();
/* 291 */         break;
/*     */       case 68:
/* 293 */         Board.this.oneLineDown();
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\DBDownload\lwjglDemo2\lwjglDemo2\Run.jar
 * Qualified Name:     com.java2dgame.tetris.Board
 * JD-Core Version:    0.6.2
 */